package com.ktdaddy.manager;

import com.ktdaddy.enums.WhaleErrorCode;
import com.ktdaddy.exception.BusinessRuntimeException;
import com.ktdaddy.mapper.DeptMapper;
import com.ktdaddy.pojo.Dept;
import com.ktdaddy.request.CreateDeptReq;
import com.ktdaddy.request.UpdateDeptReq;
import com.ktdaddy.vo.DeptVO;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: lujianhao
 * @Description: 部门管理
 * @Date: 2021/7/22 3:15 下午
 */
@Component
public class DeptManager {
    @Resource
    private DeptMapper deptMapper;

    @Autowired
    private Sid sid;

    /**
     * 子部门名称查询父部门
      * @param childName
     * @return
     */
    public Dept queryParentByChildName(String childName) {
        //查询子部门信息
        Dept childDept = queryByDeptName(childName);
        if(Objects.isNull(childDept)){
            return null;
        }
        return deptMapper.selectByPrimaryKey(childDept.getParentId());
    }

    public Dept queryByDeptName(String deptName){
        Example deptExample = new Example(Dept.class);
        Example.Criteria deptCriteria = deptExample.createCriteria();
        deptCriteria.andEqualTo("deptName",deptName);
        return deptMapper.selectOneByExample(deptExample);
    }

    /**
     * 判断是否根节点
     * @param deptName
     * @return
     */
    public boolean isRootNode(String deptName) {
        if(StringUtils.isEmpty(deptName)){
            return false;
        }
        Dept dept = queryByDeptName(deptName);
        if(Objects.isNull(dept)){
            return false;
        }
        return dept.getParentId().equals(0);
    }


    public List<DeptVO> queryRootByDeptNameAndStatus(String rootName, Integer status) {
        Example deptExample = new Example(Dept.class);
        Example.Criteria deptCriteria = deptExample.createCriteria();
        if(!StringUtils.isEmpty(rootName)){
            deptCriteria.andEqualTo("deptName",rootName);
        }
        if(status != null){
            deptCriteria.andEqualTo("status",status);
        }
        deptCriteria.andEqualTo("parentId",0);
        List<DeptVO> result = getDeptVOS(deptExample);
        return result;
    }

    private List<DeptVO> getDeptVOS(Example deptExample) {
        List<Dept> deptList = deptMapper.selectByExample(deptExample);

        List<DeptVO> result = new ArrayList<>();
        for (Dept dept : deptList) {
            DeptVO deptVO = new DeptVO();
            BeanUtils.copyProperties(dept, deptVO);
            deptVO.setCreateTime(dept.getTimeCreated());
            //查询父部门
            deptVO.setParentDept(dept.getParentId());
            result.add(deptVO);
        }
        return result;
    }

    public List<DeptVO> queryParentByDeptNameAndStatus(String rootName, Integer status) {
        Example deptExample = new Example(Dept.class);
        Example.Criteria deptCriteria = deptExample.createCriteria();
        deptCriteria.andEqualTo("deptName",rootName);
        deptCriteria.andEqualTo("status",status);

        List<DeptVO> result = getDeptVOS(deptExample);
        return result;
    }

    public List<DeptVO> queryChildByDeptNameAndStatus(Integer parentId, String deptName, Integer status) {
        Example deptExample = new Example(Dept.class);
        Example.Criteria deptCriteria = deptExample.createCriteria();
        deptCriteria.andEqualTo("parentId",parentId);
        if(!StringUtils.isEmpty(deptName)){
            deptCriteria.andEqualTo("deptName",deptName);
        }

        if(!Objects.isNull(status)){
            deptCriteria.andEqualTo("status",status);
        }

        List<DeptVO> result = getDeptVOS(deptExample);
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void saveDept(CreateDeptReq createDeptReq) {
        Dept dept = new Dept();
        BeanUtils.copyProperties(createDeptReq,dept);
        dept.setDeptCode(sid.nextShort());
        dept.setParentId(createDeptReq.getParentDept());
        Integer result = deptMapper.insertSelective(dept);
        if(result < 1){
            throw new BusinessRuntimeException(WhaleErrorCode.DB_INSERT_ERROR);
        }
    }

    public Dept queryById(Integer id) {
        return deptMapper.selectByPrimaryKey(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateDept(UpdateDeptReq updateDeptReq,Dept oldDept) {
        BeanUtils.copyProperties(updateDeptReq,oldDept);
        oldDept.setParentId(updateDeptReq.getParentDept());
        Integer result = deptMapper.updateByPrimaryKeySelective(oldDept);
        if(result < 1){
            throw new BusinessRuntimeException(WhaleErrorCode.DB_UPDATE_ERROR);
        }
    }

    public List<Dept> queryChildByParentId(Integer deptId) {
        Example deptExample = new Example(Dept.class);
        Example.Criteria deptCriteria = deptExample.createCriteria();
        deptCriteria.andEqualTo("parentId",deptId);
        List<Dept> deptList = deptMapper.selectByExample(deptExample);
        return deptList;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteById(Integer deptId) {
        deptMapper.deleteByPrimaryKey(deptId);
    }
}
